
export const CommonModal = {
  data () {
    return {
      title: '操作',
      type: '1', // 操作类型 1：新增；2：修改；3：详情
      visible: false,
      confirmLoading: false,
      form: {},
      rules: {},
      modelConfig: {
        ref: 'ruleForm',
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      }
    }
  },
  computed: {
    disabled () {
      return this.type === '3'
    }
  },
  methods: {
    add (record = {}) {
      this.title = '新增'
      this.form = { ...record }
      this.type = '1'
      this.visible = true
    },
    edit (record) {
      this.title = '编辑'
      this.form = { ...record }
      this.type = '2'
      this.visible = true
    },
    view (record) {
      this.title = '详情'
      this.form = { ...record }
      this.type = '3'
      this.visible = true
    },
    handleOk (e, refName = 'ruleForm', type) {
      if (type) {
        this.type = type
      }
      const ref = refName === 'ruleForm' ? this.$refs.ruleForm : refName.$refs.ruleForm
      ref.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          const params = { ...this.form }
          const url = this.type === '1' ? this.url.add : this.url.edit
          this.$post(url, params).then(res => {
            this.confirmLoading = false
            if (this.isSuccess(res)) {
              this.$message.success('操作成功')
              this.visible = false
              this.$emit('ModalOk')
              if (this.reloadDict) {
                this.$store.dispatch('GetDictData')
              }
            } else {
              this.$message.error('操作失败')
            }
          })
        }
      })
    }
  }
}
